package com.ct.system.responsity.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.system.mapper.AdminSystemDepartmentMapper;
import com.ct.system.orm.AdminSystemDepartment;
import com.ct.system.responsity.admin.IAdminSystemDepartmentRepository;
import com.ct.system.responsity.admin.IAdminSystemUserRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 系统部门
 *
 * @author system
 */
@Repository
public class AdminSystemDepartmentRepository extends BaseServiceImpl<AdminSystemDepartmentMapper, AdminSystemDepartment> implements IAdminSystemDepartmentRepository {


    @Resource
    private IAdminSystemUserRepository adminSystemUserRepository;

    /**
     * 查询公司部门
     *
     * @return
     */
    @Override
    public List<AdminSystemDepartment> findPageTree(Long systemId) {
        LambdaQueryWrapper<AdminSystemDepartment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystemDepartment::getSystemId, systemId);
        lambdaQueryWrapper.orderByAsc(AdminSystemDepartment::getSorted);
        List<AdminSystemDepartment> adminSystemDepartments = this.list(lambdaQueryWrapper);
        List<AdminSystemDepartment> newAdminSystemDepartments = new ArrayList<>();
        if (!CollectionUtils.isEmpty(adminSystemDepartments)) {
            // 这里就把菜单进行tree化
            Map<String, Object> stringStringMap = adminSystemUserRepository.countDepartmentById(systemId);
            for (AdminSystemDepartment adminSystemDepartment : adminSystemDepartments) {
                adminSystemDepartment.setCode("department");
                adminSystemDepartment.setCount(stringStringMap.get(String.valueOf(adminSystemDepartment.getId())));
                if (adminSystemDepartment.getParentId() == 0L) {
                    newAdminSystemDepartments.add(getMenuTree(stringStringMap,systemId,adminSystemDepartment, adminSystemDepartments));
                }
            }
        }
        return newAdminSystemDepartments;
    }

    /**
     * 根据部门ID删除
     * @param id
     * @return
     */
    @Override
    public boolean removeByDepartmentId(Long id){
        boolean b = this.removeById(id);
        if(b){
            LambdaQueryWrapper<AdminSystemDepartment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AdminSystemDepartment::getParentId,id);
            this.remove(lambdaQueryWrapper);
            return true;
        }
        return  false;
    }


    /**
     * tree的方法
     *
     * @param result
     * @param allList
     * @return
     */
    private AdminSystemDepartment getMenuTree(Map<String, Object> stringStringMap,Long systemId,AdminSystemDepartment result, List<AdminSystemDepartment> allList) {
        for (AdminSystemDepartment adminSystemDepartment : allList) {
            adminSystemDepartment.setCode("department");
            adminSystemDepartment.setCount(stringStringMap.get(String.valueOf(adminSystemDepartment.getId())));
            if (Objects.equals(adminSystemDepartment.getParentId(), result.getId())) {
                if (result.getChildren() == null) {
                    result.setChildren(new ArrayList<>());
                }
                result.getChildren().add(getMenuTree(stringStringMap,systemId,adminSystemDepartment, allList));
            }
        }
        return result;
    }
}
